package com.cet.programmercarl.algorithmperformancanalysis.二叉树.层级遍历;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * @program: algorithm
 * @description: n叉树层级遍历
 * @author: 陈恩涛
 * @create: 2022-07-18 15:36
 **/
public class LC429 {

    public static void main(String[] args) {

    }

    public List<List<Integer>> levelOrder(Node root) {
        final List<List<Integer>> result = new ArrayList<>();
        if (root == null) {
            return result;
        }
        final Queue<Node> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            final List<Integer> list = new ArrayList<>();
            while (size > 0) {
                final Node cur = queue.poll();
                list.add(cur.val);
                for (Node child : cur.children) {
                    queue.offer(child);
                }
                size--;
            }
            result.add(list);
        }
        return result;
    }
}

class Node {
    public int val;
    public List<Node> children;

    public Node() {}

    public Node(int _val) {
        val = _val;
    }

    public Node(int _val, List<Node> _children) {
        val = _val;
        children = _children;
    }
}
